package com.train.controller;

import com.train.dto.EmployeeDTO;
import com.train.pojo.Department;
import com.train.pojo.Employee;
import com.train.service.DepartmentService;
import com.train.service.EmployeeService;
import com.train.tools.DateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;

@Controller
public class EmployeeController {

    @Autowired
    private DateFormat dateFormat;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/findEmp")
    public String find(@RequestParam(value = "employeename",required = false) String employeename,
                       @RequestParam(value = "idcard",required = false)String idcard,
                       @RequestParam(value = "sex",required = false)Integer sex,
                       @RequestParam(value = "age",required = false)Integer age,
                       @RequestParam(value = "telephone",required = false)String telephone,
                       @RequestParam(value = "address",required = false)String address,
                       @RequestParam(value = "email",required = false)String email,
                       @RequestParam(value = "departmentname",required = false)String departmentname,
                       @RequestParam(value = "position",required = false)String position,
                       @RequestParam(value = "date",required = false)String str,
                       Model model) {
        EmployeeDTO employeeDTO=new EmployeeDTO();
        if (employeename!=null && employeename.length()!=0){
            employeeDTO.setEmployeename(employeename);
            System.out.println("employeename:"+employeename);
        }
        if (idcard!=null && idcard.length()!=0){
            employeeDTO.setIdcard(idcard);
            System.out.println("idcard:"+idcard);
        }
        if (sex!=null){
            employeeDTO.setSex(sex);
            System.out.println("sex:"+sex);
        }
        if (age!=null){
            employeeDTO.setAge(age);
            System.out.println("age:"+age);
        }
        if (telephone!=null && telephone.length()!=0){
            employeeDTO.setTelephone(telephone);
            System.out.println("telephone:"+telephone);
        }
        if (address!=null && address.length()!=0){
            employeeDTO.setAddress(address);
            System.out.println("address:"+address);
        }
        if (email!=null && email.length()!=0){
            employeeDTO.setEmail(email);
            System.out.println("email:"+email);
        }
        if (departmentname!=null && departmentname.length()!=0){
            employeeDTO.setDepartmentname(departmentname);
            System.out.println("address:"+departmentname);
        }
        if (position!=null && position.length()!=0){
            employeeDTO.setPosition(position);
            System.out.println("position:"+position);
        }
        if (str!=null && str.length()!=0){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date d=null;
            try {
                d =formatter.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            java.sql.Date date=new java.sql.Date(d.getTime());
            employeeDTO.setDate(date);
            System.out.println("date:"+date);
        }

        Collection<EmployeeDTO> employees=employeeService.findByRequired(employeeDTO);
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("emps",employees);
        model.addAttribute("departments",departments);
        return "emp/findEmpList.html";
    }

    @GetMapping("/userEmp")
    public String userEmp(@ModelAttribute("msg")String msg,@ModelAttribute("msg")String msgCode,Model model){
//        Collection<EmployeeDTO> employees = employeeService.selectAllEmployeeDTO();
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("departments",departments);
//        model.addAttribute("emps",employees);
        return "emp/findEmpList.html";
    }

    //查询所有员工，返回列表页面
    @GetMapping("/emp")
    public String list(@ModelAttribute("msg")String msg,@ModelAttribute("msg")String msgCode,Model model){
        Collection<EmployeeDTO> employees = employeeService.selectAllEmployeeDTO();
        Collection<Department> departments = departmentService.selectAllDepartment();
        System.out.println("msg"+msg);
        System.out.println("msgCode"+msgCode);
        model.addAttribute("departments",departments);
        model.addAttribute("emps",employees);
        return "emp/empList.html";
    }

    //to员工添加页面
    @GetMapping("/addEmp")
    public String toAdd(@ModelAttribute("msg")String msg,@ModelAttribute("msg")String msgCode,Model model){
        System.out.println("msg"+msg);
        System.out.println("msgCode"+msgCode);
        //查出所有的部门，提供选择
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("departments",departments);
        return "emp/addEmp.html";
    }

    //员工添加功能，使用post接收
    @PostMapping("/addEmp")
    public String add(Employee employee, Model model,RedirectAttributes rettr){
        String idcard=employee.getIdcard();
        Employee emp=employeeService.selectEmployeeByIdcard(idcard);
        if (emp!=null){
            rettr.addFlashAttribute("msg","该身份证号已存在");
            rettr.addFlashAttribute("msgCode","500");
            return "redirect:/addEmp";
        }else {
            employeeService.addEmployee(employee);
            //回到员工列表页面，可以使用redirect或者forward
            rettr.addFlashAttribute("msg","员工添加成功");
            rettr.addFlashAttribute("msgCode","200");
            return "redirect:/emp";
        }
    }

    //to员工修改页面
    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id") Integer id, Model model){
        //根据id查出来员工
        Employee employee = employeeService.selectEmployeeById(id);
        //将员工信息返回页面
        model.addAttribute("emp",employee);
        //查出所有的部门，提供修改选择
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("departments",departments);

        return "emp/updateEmp.html";
    }

    @PostMapping("/updateEmp")
    public String updateEmp(Employee employee,RedirectAttributes rettr){
        employeeService.updateEmployee(employee);
        rettr.addFlashAttribute("" +
                "msg","员工修改成功");
        rettr.addFlashAttribute("msgCode","200");
        //回到员工列表页面
        return "redirect:/emp";
    }

    @GetMapping("/delEmp/{id}")
    public String deleteEmp(@PathVariable("id")Integer id,RedirectAttributes rettr){
        //根据id删除员工
        employeeService.deleteEmployee(id);
        rettr.addFlashAttribute("" +
                "msg","员工删除成功");
        rettr.addFlashAttribute("msgCode","200");
        return "redirect:/emp";
    }

}
